import { defineStore } from "pinia";
import { PLAYERS } from "@/utils/global";

const initPlayer = (role = 1) => {
  const pr = PLAYERS[`player${role}`]

  return {
    id: `player${role}`,
    role, // 1(player1) or 2(player2)
    moving: false, // 是否处于移动状态
    canMove: false, // 是否可以移动
    color: pr.color,
    x: pr.x,
    y: pr.y,
    hp: PLAYERS.hp,
    speed: PLAYERS.speed,
    power: PLAYERS.power, // BULLET.level[power]
    canFire: false, // 是否可以开火
    protection: false, // 是否展示头盔效果
    protectionId: '',
    direction: PLAYERS.direction,
    shape: PLAYERS.shape,
    score: 0,
    killInfo: {
      basic: 0,
      fast: 0,
      power: 0,
      armor: 0
    }
  }
}

export const usePlayerStore = defineStore('player', {
  state: () => ({
    players: new Map(),
  }),
  actions: {
    bornPlayer(role = 1) {
      const o = {
        ...initPlayer(role),
        canMove: true,
        canFire: true,
      }

      this.players.set(role, o)
    },
    updatePlayer({ role = 1, ...params }) {
      const item = this.players.get(role)
      this.players.set(role, { ...item, ...params })
    },
    clearPlayers() {
      this.players.clear()
    },
    getPlayer(role) {
      return this.players.get(role)
    }
  }
})